package com.forlove.app.qselect.friend.entity;

import android.support.annotation.NonNull;

import com.forlove.app.common.base.mvp.entity.BaseEntity;
import com.forlove.app.common.base.serializer.JSONKey;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.qselect.login.entity.ContactsInfoEntity;

import java.text.Collator;
import java.util.Comparator;

/**
 * 我的好友界面好友列表，重写比较接口方法
 * Created by yjl on 2017/11/8.
 */

public class MineFriendEntity extends BaseEntity implements Comparable<MineFriendEntity> {
    @JSONKey(keys = "address", type = Integer.class)
    public int address; //地址代码

    @JSONKey(keys = "addtime", type = Long.class)
    public long addtime; //添加时间

    @JSONKey(type = Integer.class, keys = "birthday")
    public int birthday;

    @JSONKey(keys = "group_tag", type = Integer.class)
    public int group_tag;

    @JSONKey(keys = "img", type = String.class)
    public String img = "";//好友头像

    @JSONKey(keys = "issingle", type = Integer.class)
    public int issingle; //已婚还是单身

    @JSONKey(keys = "lastloc", type = String.class)
    public String lastloc;

    @JSONKey(keys = "mobile", type = String.class)
    public String mobile = "";//手机号码

    @JSONKey(keys = "realname", type = String.class)
    public String realname = "";  //真实姓名

    @JSONKey(keys = "realname_fc", type = String.class)
    public String realname_fc = "";  //真实姓名首字母

    @JSONKey(keys = "scholar", type = String.class)
    public String scholar;  //年纪

    @JSONKey(keys = "sex", type = Integer.class)
    public int sex;//1男，2女  性别

    @JSONKey(keys = "isfriend", type = Boolean.class)
    public boolean isfriend = false;  //是否是好友，添加通讯录好友返回

    @JSONKey(keys = "user_id", type = Integer.class)
    public int user_id;   //圈一圈id

    @JSONKey(type = String.class, keys = "username")
    public String username = "";  //圈一圈账户名

    @JSONKey(type = String.class, keys = "username_fc")
    public String username_fc;//圈一圈账户名首字母

    @JSONKey(keys = "msg", type = String.class)
    public String msg;

    @JSONKey(type = Integer.class, keys = "status")
    public int status = -1;// 0 发好友请求，需要同意， 1 已经添加好友， -1 注册未添加好友，新的好友中userid为0则为未注册用户

    @JSONKey(keys = "canInvite", type = Boolean.class)
    public boolean canInvite = true;  //是否可以邀请

    @JSONKey(type = String.class, keys = "concatname")
    public String mConcatName = "";  //服务器返回的通讯录好友名称

    @JSONKey(keys = "charm", type = Integer.class)
    public int charm;  //好友的魅力值

    @JSONKey(type = Integer.class, keys = "commonFriends")
    public int commonFriends;  //共同好友


    //本地判断的两个字段
    @JSONKey(keys = "isFromContacts", type = Boolean.class)
    public boolean isFromContacts = false;//是否来自通讯录
    //本地通讯录的字段
    @JSONKey(type = String.class, keys = "contactsName")
    public String contactsName = "";//通讯录姓名 需要本地获取
    @JSONKey(type = String.class, keys = "contactsNumber")
    public String contactsNumber = "";  //通讯录的号码

    @JSONKey(type = String.class, keys = "sortKey")
    public String sortKey = realname_fc;   //通讯录的排序的key

    @JSONKey(type = Integer.class, keys = "contactsid")
    public int contactsid;  //通讯录的id

    @JSONKey(type = Integer.class, keys = "diamond")
    public int diamond;     //钻石数


    /***本地字段,解析时设置***/
    @JSONKey(type = Boolean.class, keys = "localRecomend")
    public boolean isRecomend = false; //是否是推荐好友

    /****本地解析判断字段，答题时当前题目是否被圈选过****/
    public boolean hasCircle = false;//是否被圈选过

    /*****指定候选人，是否被选中，本地字段，指定候选人时使用****/
    public boolean isSelected = false; //

    public void copyContactEntity(ContactsInfoEntity contactsInfoEntity) {
        this.contactsName = contactsInfoEntity.getName();
        this.contactsNumber = contactsInfoEntity.getNumber();
        this.sortKey = contactsInfoEntity.getSortKey();
        this.contactsid = contactsInfoEntity.getId();
        this.isFromContacts = true;
    }

    /**
     * 拷贝系统返回的好友信息部分
     */
    public void copyFriendEnity(MineFriendEntity friendEntity) {
        this.address = friendEntity.address;
        this.addtime = friendEntity.addtime;
        this.birthday = friendEntity.birthday;
        this.group_tag = friendEntity.group_tag;
        this.img = friendEntity.img;
        this.issingle = friendEntity.issingle;
        this.lastloc = friendEntity.lastloc;
        this.mobile = friendEntity.mobile;
        this.realname = friendEntity.realname;
        this.realname_fc = friendEntity.realname_fc;
        this.scholar = friendEntity.scholar;
        this.sex = friendEntity.sex;
        this.isfriend = friendEntity.isfriend;
        this.user_id = friendEntity.user_id;
        this.username = friendEntity.username;
        this.username_fc = friendEntity.username_fc;
        this.msg = friendEntity.msg;
        this.status = friendEntity.status;
        this.commonFriends = friendEntity.commonFriends;
        this.canInvite = friendEntity.canInvite;
        this.charm = friendEntity.charm;
    }

    public MineFriendEntity() {
    }

    /**
     * 本地通讯录和好友转换
     *
     * @param contactsName
     * @param contactsNumber
     * @param sortKey
     * @param contactsid
     */
    public MineFriendEntity(String contactsName, String contactsNumber, String sortKey, int contactsid) {
        this.contactsName = contactsName;
        this.contactsNumber = contactsNumber;
        this.sortKey = sortKey;
        this.contactsid = contactsid;
        this.isFromContacts = true;
    }

    /**
     * ContactsInfoEntity 和好友转换
     *
     * @param contactsInfoEntity
     */
    public MineFriendEntity(ContactsInfoEntity contactsInfoEntity) {
        this.contactsName = contactsInfoEntity.getName();
        this.contactsNumber = contactsInfoEntity.getNumber();
        this.sortKey = contactsInfoEntity.getSortKey();
        this.contactsid = contactsInfoEntity.getId();
        this.isFromContacts = true;
    }

    public MineFriendEntity(MineFriendEntity mineFriendEntity) {
        this.address = mineFriendEntity.address;
        this.addtime = mineFriendEntity.addtime;
        this.birthday = mineFriendEntity.birthday;
        this.group_tag = mineFriendEntity.group_tag;
        this.img = mineFriendEntity.img;
        this.issingle = mineFriendEntity.issingle;
        this.lastloc = mineFriendEntity.lastloc;
        this.mobile = mineFriendEntity.mobile;
        this.realname = mineFriendEntity.realname;
        this.realname_fc = mineFriendEntity.realname_fc;
        this.scholar = mineFriendEntity.scholar;
        this.sex = mineFriendEntity.sex;
        this.isfriend = mineFriendEntity.isfriend;
        this.user_id = mineFriendEntity.user_id;
        this.username = mineFriendEntity.username;
        this.username_fc = mineFriendEntity.username_fc;
        this.msg = mineFriendEntity.msg;
        this.status = mineFriendEntity.status;
        this.canInvite = mineFriendEntity.canInvite;
        this.mConcatName = mineFriendEntity.mConcatName;
        this.charm = mineFriendEntity.charm;
        this.commonFriends = mineFriendEntity.commonFriends;
        this.isFromContacts = mineFriendEntity.isFromContacts;
        this.contactsName = mineFriendEntity.contactsName;
        this.contactsNumber = mineFriendEntity.contactsNumber;
        this.sortKey = mineFriendEntity.sortKey;
        this.contactsid = mineFriendEntity.contactsid;
        this.isRecomend = mineFriendEntity.isRecomend;
        this.hasCircle = mineFriendEntity.hasCircle;
    }

    @Override
    public int compareTo(@NonNull MineFriendEntity o) {
        Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
        return cmp.compare(this.realname, o.realname);
    }

    /**
     * 返回可以显示的名称
     *
     * @return
     */
    public String getAvaliableName() {
        if (StringUtils.isNotEmpty(this.realname)) {
            return this.realname;
        } else if (StringUtils.isNotEmpty(this.contactsName)) {
            return this.contactsName;

        } else if (StringUtils.isNotEmpty(this.mConcatName)) {
            return this.mConcatName;
        } else if (StringUtils.isNotEmpty(this.username)) {
            return this.username;
        } else if (StringUtils.isNotEmpty(this.contactsNumber)) {
            return this.contactsNumber;
        } else if (StringUtils.isNotEmpty(mobile)) {
            return this.mobile;
        } else {
            return " ";
        }
    }


    public String getAvaliableMobile() {
        if (StringUtils.isNotEmpty(this.mobile)) {
            return this.mobile;
        }
        return this.contactsNumber;
    }


    /**
     * 优先获取通讯录名称
     *
     * @return
     */
    public String getContactsName() {
        if (StringUtils.isNotEmpty(this.contactsName)) {
            return this.contactsName;
        } else if (StringUtils.isNotEmpty(mConcatName)) {
            return mConcatName;
        } else if (StringUtils.isNotEmpty(realname)) {
            return realname;
        } else if (StringUtils.isNotEmpty(this.contactsNumber)) {
            return this.contactsNumber;
        } else if (StringUtils.isNotEmpty(mobile)) {
            return this.mobile;
        } else {
            return " ";
        }
    }

    public boolean getSearchStatus(String searchStatus) {
        if (!StringUtils.isNotEmpty(searchStatus)) {
            return false;
        }
        if (StringUtils.isNotEmpty(username) && username.contains(searchStatus)) {
            return true;
        } else if (StringUtils.isNotEmpty(getAvaliableMobile()) && getAvaliableMobile().contains(searchStatus)) {
            return true;
        }
        return false;
    }
}
